package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

import java.util.*;

class IntersectionOfTwoArraysIi {
    // 2022-08-22 17:28:46
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] intersect(int[] nums1, int[] nums2) {
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int j : nums1) {
                map.put(j, map.getOrDefault(j, 0) + 1);
            }
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < nums2.length; i++) {
                Integer count = map.getOrDefault(nums2[i], 0);
                if (count > 0) {
                    list.add(nums2[i]);
                    map.put(nums2[i], map.get(nums2[i]) - 1);
                }
            }
            int[] ans = new int[list.size()];
            final int[] index = {0};
            list.forEach(l -> {
                ans[index[0]] = l;
                index[0]++;
            });
            return ans;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}